package com.hcj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcj.pojo.Customer;
import com.hcj.service.CustomerService;
import com.hcj.util.MyResponse;
import com.hcj.util.VerifyCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService service;
    @Autowired
    private VerifyCode verifyCodeTrue;
    @RequestMapping(value = "/login")
    public MyResponse login(Customer customer, String verifyCode){
        System.out.println("verifyCode = " + verifyCode);
        System.out.println("customers = " + customer);

        /**
         *  后台不做验证码验证
         */
        if(verifyCode!=null){
            /**
             * 验证码验证
             * 用户输入的验证码:verifyCode
             * 工具类：真正的验证码：verifyCodeTrue
             * 验证码不区分大小写
             */
            String text = verifyCodeTrue.getText();
            boolean equals = verifyCode.toLowerCase().equals(text.toLowerCase());
            System.out.println("equals = " + equals);

            if(!equals){
                return new MyResponse("200","验证码错误");
            }
        }
        if(service.login(customer)){
            //登录标识
            Customer customer1 = service.selectOneByName(customer);
            System.out.println("customer1 = " + customer1);
            return new MyResponse("100",customer1);
        }else
            return new MyResponse("200","用户名或密码错误");
    }
    @RequestMapping(value = "/register",params = {"username","password","pwd"})
    public MyResponse register(Customer customers,String pwd){
        //判断两次密码是否一致
        if (!pwd.equals(customers.getPassword())){
            return new MyResponse("200","两次密码不一致");
        }
        MyResponse register = service.register(customers);
        return register;
    }
    @RequestMapping("/delete")
    public MyResponse delete(Customer customers){
        return service.delete(customers);
    }
    @RequestMapping("/selectByLikeName")
    public List<Customer> selectByLikeName(String username){
        System.out.println("username = " + username);
        return service.selectByName(username);
    }
    @RequestMapping("/selectAll")
    public Page<Customer> selectAllPage(Integer currentPage, Integer pageSize){
        return service.selectAllPaging(currentPage,pageSize);
    }
    @RequestMapping("/update")
    public MyResponse update(Customer customer){
        return service.update(customer);

    }


}
